home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Dogs / Multimedia Dogs v2.0.iso / pc / demos / petdisc / mmpets1d.exe / MMPETS1D.DXR / 00206_Database handlers.ls < prev    next >
Encoding:
Text File  |  1995-10-06  |  9.5 KB  |  292 lines

  1. global gBreedDatabase, gBreedDatabaseCount, gBreedDatabaseByGroup, gBreedDatabaseByWorld, gBreedDatabaseByCode, gGalleryDatabase, gGalleryDatabaseCount, gQuizDatabase, gQuizDatabaseCount, gGlossaryDatabase, gGlossaryDatabaseCount
  2.  
  3. on initDatabases
  4.   initBreedDatabase()
  5.   initGalleryDatabase()
  6.   initGlossaryDatabase()
  7.   initQuizDatabase()
  8. end
  9.  
  10. on getBreedIndex theCode
  11.   repeat with i = 1 to gBreedDatabaseCount
  12.     set theRecord to getAt(gBreedDatabase, i)
  13.     if getAt(theRecord, 2) = theCode then
  14.       return i
  15.     end if
  16.   end repeat
  17.   alert("Bogus code for breed:" && theCode)
  18.   return -1
  19. end
  20.  
  21. on getBreedRecord theIndex
  22.   if (theIndex > gBreedDatabaseCount) or (theIndex < 0) then
  23.     return EMPTY
  24.   else
  25.     return getAt(gBreedDatabase, theIndex)
  26.   end if
  27. end
  28.  
  29. on getFieldDisplayName theRecord
  30.   return getAt(theRecord, 1)
  31. end
  32.  
  33. on getFieldBreedCode theRecord
  34.   return getAt(theRecord, 2)
  35. end
  36.  
  37. on getFieldGroupCode theRecord
  38.   return getAt(theRecord, 3)
  39. end
  40.  
  41. on getFieldProfile theRecord
  42.   return getAt(theRecord, 4)
  43. end
  44.  
  45. on genAll
  46.   genBreedCode()
  47.   genGalleryCode()
  48.   genGlossaryCode()
  49.   genQuizCode()
  50.   genTopicsText()
  51. end
  52.  
  53. on genBreedCode
  54.   put "Generating initBreedDatabase()..."
  55.   set fname to the pathName & "data:zsupport:database:breeds.txt"
  56.   set theCastName to "BreedDatabaseCode"
  57.   set file to FileIO(mnew, "read", fname)
  58.   if not objectp(file) then
  59.     alert("Fatal error: cannot read file" && fname)
  60.     exit
  61.   end if
  62.   set skipString to TAB & RETURN & " "
  63.   set breakString to TAB & RETURN
  64.   set theDatabase to []
  65.   repeat while 1
  66.     set token to file(mReadToken, breakString, skipString)
  67.     if token = EMPTY then
  68.       exit repeat
  69.     end if
  70.     if char 1 of token = "#" then
  71.       file(mReadLine)
  72.       next repeat
  73.     end if
  74.     put "reading" && token
  75.     set theRecord to [token]
  76.     set token to file(mReadToken, breakString, skipString)
  77.     append(theRecord, token)
  78.     set token to file(mReadToken, breakString, skipString)
  79.     append(theRecord, token)
  80.     set theProfile to []
  81.     repeat with i = 1 to 8
  82.       set token to file(mReadToken, breakString, skipString)
  83.       append(theProfile, value(token))
  84.     end repeat
  85.     append(theRecord, theProfile)
  86.     append(theDatabase, theRecord)
  87.   end repeat
  88.   file(mdispose)
  89.   put "Building indicies..."
  90.   set codeIndex to makeIndex(theDatabase, 2)
  91.   set groupIndex to makeIndex(theDatabase, 3)
  92.   set theString to EMPTY
  93.   set theString to theString & "-- This code was generated by genBreedCode()" & RETURN & RETURN
  94.   set theString to theString & "global gBreedDatabase" & RETURN
  95.   set theString to theString & "global gBreedDatabaseCount" & RETURN
  96.   set theString to theString & "global gBreedDatabaseByCode" & RETURN
  97.   set theString to theString & "global gBreedDatabaseByGroup" & RETURN & RETURN
  98.   set theString to theString & "on initBreedDatabase" & RETURN & RETURN
  99.   set theString to theString & "set gBreedDatabase =" && theDatabase & RETURN & RETURN
  100.   set theString to theString & "set gBreedDatabaseCount = count(gBreedDatabase)" & RETURN & RETURN
  101.   set theString to theString & "set gBreedDatabaseByCode  =" && codeIndex & RETURN & RETURN
  102.   set theString to theString & "set gBreedDatabaseByGroup =" && groupIndex & RETURN & RETURN
  103.   set theString to theString & "end" & RETURN
  104.   set the scriptText of cast theCastName to theString
  105.   put "Done."
  106.   beep()
  107. end
  108.  
  109. on genGalleryCode
  110.   put "Generating initGalleryDatabase()..."
  111.   set fname to the pathName & "data:zsupport:database:gallery.txt"
  112.   set theCastName to "GalleryDatabaseCode"
  113.   set file to FileIO(mnew, "read", fname)
  114.   if not objectp(file) then
  115.     alert("Fatal error: cannot read file" && fname)
  116.     exit
  117.   end if
  118.   set skipString to TAB & RETURN & " "
  119.   set breakString to TAB & RETURN
  120.   set theDatabase to []
  121.   repeat while 1
  122.     set token to file(mReadToken, breakString, skipString)
  123.     if token = EMPTY then
  124.       exit repeat
  125.     end if
  126.     if char 1 of token = "#" then
  127.       file(mReadLine)
  128.       next repeat
  129.     end if
  130.     set theRecord to [token]
  131.     set token to file(mReadToken, breakString, skipString)
  132.     append(theRecord, token)
  133.     append(theDatabase, theRecord)
  134.   end repeat
  135.   file(mdispose)
  136.   set propList to [:]
  137.   repeat with i = 1 to count(theDatabase)
  138.     set theRecord to getAt(theDatabase, i)
  139.     addProp(propList, getAt(theRecord, 1), [getAt(theRecord, 2), "gallery"])
  140.   end repeat
  141.   initBreedDatabase()
  142.   repeat with i = 1 to gBreedDatabaseCount
  143.     set theRecord to getAt(gBreedDatabase, i)
  144.     addProp(propList, getFieldDisplayName(theRecord), [getFieldBreedCode(theRecord), getFieldGroupCode(theRecord)])
  145.   end repeat
  146.   sort(propList)
  147.   set theList to []
  148.   repeat with i = 1 to count(propList)
  149.     set theRecord to getAt(propList, i)
  150.     set theProp to getPropAt(propList, i)
  151.     append(theList, [theProp, getAt(theRecord, 1), getAt(theRecord, 2)])
  152.   end repeat
  153.   set theString to EMPTY
  154.   set theString to theString & "-- This code was generated by genGalleryCode()" & RETURN & RETURN
  155.   set theString to theString & "global gGalleryDatabase" & RETURN
  156.   set theString to theString & "global gGalleryDatabaseCount" & RETURN & RETURN
  157.   set theString to theString & "on initGalleryDatabase" & RETURN & RETURN
  158.   set theString to theString & "set gGalleryDatabase =" && theList & RETURN & RETURN
  159.   set theString to theString & "set gGalleryDatabaseCount = count(gGalleryDatabase)" & RETURN & RETURN
  160.   set theString to theString & "end" & RETURN
  161.   set the scriptText of cast theCastName to theString
  162.   put "Done."
  163.   beep()
  164. end
  165.  
  166. on genGlossaryCode
  167.   put "Generating initGlossaryDatabase()..."
  168.   set fname to the pathName & "data:zsupport:database:glossary.txt"
  169.   set theCastName to "GlossaryDatabaseCode"
  170.   set theListname to "GlossaryList"
  171.   set file to FileIO(mnew, "read", fname)
  172.   if not objectp(file) then
  173.     alert("Fatal error: cannot read file" && fname)
  174.     exit
  175.   end if
  176.   set skipString to TAB & RETURN & " "
  177.   set breakString to TAB & RETURN
  178.   set theDatabase to []
  179.   set theList to EMPTY
  180.   repeat while 1
  181.     set token to file(mReadToken, breakString, skipString)
  182.     if token = EMPTY then
  183.       exit repeat
  184.     end if
  185.     if char 1 of token = "#" then
  186.       file(mReadLine)
  187.       next repeat
  188.     end if
  189.     put "reading" && token
  190.     set theRecord to [token]
  191.     if theList = EMPTY then
  192.       set theList to theList & token
  193.     else
  194.       set theList to theList & RETURN & token
  195.     end if
  196.     set token to file(mReadToken, breakString, skipString)
  197.     append(theRecord, munge(token))
  198.     append(theDatabase, theRecord)
  199.   end repeat
  200.   file(mdispose)
  201.   set theString to EMPTY
  202.   set theString to theString & "-- This code was generated by genGlossaryCode()" & RETURN & RETURN
  203.   set theString to theString & "global gGlossaryDatabase" & RETURN
  204.   set theString to theString & "global gGlossaryDatabaseCount" & RETURN & RETURN
  205.   set theString to theString & "on initGlossaryDatabase" & RETURN & RETURN
  206.   set theString to theString & "set gGlossaryDatabase =" && theDatabase & RETURN & RETURN
  207.   set theString to theString & "set gGlossaryDatabaseCount = count(gGlossaryDatabase)" & RETURN & RETURN
  208.   set theString to theString & "end" & RETURN
  209.   set the scriptText of cast theCastName to theString
  210.   set the text of cast theListname to theList
  211.   put "Done."
  212.   beep()
  213. end
  214.  
  215. on genQuizCode
  216.   put "Generating initQuizDatabase()..."
  217.   set fname to the pathName & "data:zsupport:database:quiz.txt"
  218.   set theCastName to "QuizDatabaseCode"
  219.   set file to FileIO(mnew, "read", fname)
  220.   if not objectp(file) then
  221.     alert("Fatal error: cannot read file" && fname)
  222.     exit
  223.   end if
  224.   set skipString to TAB & RETURN & " "
  225.   set breakString to TAB & RETURN
  226.   set theDatabase to []
  227.   repeat while 1
  228.     set token to file(mReadToken, breakString, skipString)
  229.     if token = EMPTY then
  230.       exit repeat
  231.     end if
  232.     if char 1 of token = "#" then
  233.       file(mReadLine)
  234.       next repeat
  235.     end if
  236.     put "reading" && token
  237.     set theRecord to []
  238.     set token to file(mReadToken, breakString, skipString)
  239.     append(theRecord, token)
  240.     set token to file(mReadToken, breakString, skipString)
  241.     append(theRecord, munge(token))
  242.     append(theDatabase, theRecord)
  243.   end repeat
  244.   file(mdispose)
  245.   set theString to EMPTY
  246.   set theString to theString & "-- This code was generated by genQuizCode()" & RETURN & RETURN
  247.   set theString to theString & "global gQuizDatabase" & RETURN
  248.   set theString to theString & "global gQuizDatabaseCount" & RETURN & RETURN
  249.   set theString to theString & "on initQuizDatabase" & RETURN & RETURN
  250.   set theString to theString & "set gQuizDatabase =" && theDatabase & RETURN & RETURN
  251.   set theString to theString & "set gQuizDatabaseCount = count(gQuizDatabase)" & RETURN & RETURN
  252.   set theString to theString & "end" & RETURN
  253.   set the scriptText of cast theCastName to theString
  254.   put "Done."
  255.   beep()
  256. end
  257.  
  258. on makeIndex theDatabase, theKeyPosition
  259.   set theIndexList to [:]
  260.   repeat with i = 1 to count(theDatabase)
  261.     set theRecord to getAt(theDatabase, i)
  262.     set theKey to getAt(theRecord, theKeyPosition)
  263.     set theList to getaProp(theIndexList, theKey)
  264.     if voidp(theList) then
  265.       addProp(theIndexList, theKey, [i])
  266.       next repeat
  267.     end if
  268.     append(theList, i)
  269.     setProp(theIndexList, theKey, theList)
  270.   end repeat
  271.   sort(theIndexList)
  272.   return theIndexList
  273. end
  274.  
  275. on munge theString
  276.   set theNewString to EMPTY
  277.   set found to 0
  278.   repeat with i = 1 to length(theString)
  279.     set theChar to char i of theString
  280.     if theChar <> QUOTE then
  281.       if theChar = TAB then
  282.         set theChar to " "
  283.       end if
  284.       if (theChar <> " ") or (found = 1) then
  285.         set found to 1
  286.         set theNewString to theNewString & theChar
  287.       end if
  288.     end if
  289.   end repeat
  290.   return theNewString
  291. end
  292.